package com.runlooker.runer.util;

import java.net.URLEncoder;
import java.util.Map;

public class HttpUtil {
	
	
	
	
	/**
	 * @param Sring
	 *            urlstr,url字串
	 * @param Map
	 *            <String, String>params 参数以map形式封装起来
	 * @return 在get方式访问网络时把url和map参数 组合成url?param1=XXX&param2=XXX的字串类型
	 */
	public static String createGetUrl(final String urlstr,
			final Map<String, String> map) {
		StringBuilder sb = new StringBuilder(urlstr);
		int len = 0;
		int mapsize = 0;
		if (map != null && map.size() > 0) {
			sb.append("?");
			mapsize = map.size();
			for (String key : map.keySet()) {
				try {
					len++;
					sb.append(key);
					sb.append("=");
					sb.append(URLEncoder.encode(map.get(key).trim(), "utf-8"));
					if (len < mapsize) {
						sb.append("&");
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return sb.toString().trim();
	}
}
